home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / xlib / yakems11 / yaklist.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-04  |  5.1 KB  |  190 lines

  1. #include "stddefs.h"
  2. #include "yaklist.h"
  3.  
  4. //This constructor simply sets the next, previous, and data pointers to NULL
  5. pyListNode::pyListNode()
  6. {
  7.     pnNext = pnPrev = NULL;
  8.     pyData = NULL;
  9. };
  10.  
  11. //this constructor is a little more interesting, inserting this node
  12. //between pnNextNode->pnPrev and pnNextNode.
  13. pyListNode::pyListNode(yakObject * pyThisData, pyListNode * pnNextNode) : //inserts thisData's
  14. pyData(pyThisData)                     //node before pnNextNode
  15. {
  16.     pnNext = pnNextNode;
  17.     pnPrev = pnNextNode->pnPrev;
  18.     pnNextNode->pnPrev = this;
  19.     pnPrev->pnNext = this;
  20. };
  21.  
  22. //this destructor JUST deletes the data in pyData.  Some have argued that
  23. //the destructor should also remove the node from the list; I disagree,
  24. //as I feel that shouldn't be done so automatically.
  25. pyListNode::~pyListNode(void)
  26. {
  27.     if (pyData)
  28.         delete pyData;
  29. };
  30.  
  31. pyList::pyList()
  32. {
  33.     nHead.pnNext = nTail.pnNext = &nTail; //nHead and nTail point to each other
  34.     nHead.pnPrev = nTail.pnPrev = &nHead; //so we have meaningful nodes in an
  35.     iNumberInList = 0;
  36. }                     //empty list
  37.  
  38. //this destructor removes each node from the list and then deletes the
  39. //contents of that node.  Note that real objects (not created by pointers)
  40. //will also be trashed.  BE AWARE OF THIS!
  41. pyList::~pyList()
  42. {
  43.     while (nHead.pnNext != &nTail)
  44.         removeTail();
  45.     nHead.pnNext = &nTail;
  46.     nTail.pnPrev = &nHead;
  47.     iNumberInList = 0;
  48. }
  49.  
  50. //returns the object at the head of the list
  51. yakObject * pyList::pyHeadData()
  52. {
  53.     return nHead.pnNext->pyData;
  54. };
  55.  
  56. //returns the object at the tail of the list
  57. yakObject * pyList::pyTailData()
  58. {
  59.     return nTail.pnPrev->pyData;
  60. };
  61.  
  62. //adds a node at the head (beginning) of a list
  63. void pyList::addHead(yakObject * newData)
  64. {
  65.     new pyListNode(newData, nHead.pnNext);
  66.     iNumberInList++;
  67. };
  68.  
  69. //adds a node at the tail (end) of a list
  70. void pyList::addTail(yakObject * newData)
  71. {
  72.     new pyListNode(newData, &nTail);
  73.     iNumberInList++;
  74. };
  75.  
  76. //adds a node, using the pyListNode constructor.  This is, arguably,
  77. //a syntactic preference; use would be something like:
  78. //myPyList.addNode(new pyListNode(new yakObject,
  79. //                                 new pyListNode(new yakObject,
  80. //                                 &myPyList.nTail)));
  81. //making it chainable.
  82. void pyList::addNode(pyListNode * pnNextNode)
  83. {
  84.     if(pnNextNode)
  85.         NULL;
  86. }
  87.  
  88. //removes and deletes the last item in the list.  If the node's bIsTemporary
  89. //is set to a nonzero value, does not delete the item.
  90. void pyList::removeTail()
  91. {
  92.     pyListNode * pnTemp = nTail.pnPrev;
  93.     nTail.pnPrev = pnTemp->pnPrev;
  94.     pnTemp->pnPrev->pnNext = &nTail;
  95.     if (pnTemp->pyData->bIsTemporary && (pnTemp != &nHead))
  96.         delete pnTemp;
  97.     iNumberInList--;
  98. }
  99.  
  100. //removes and deletes the first item in the list, similar to removeTail
  101. void pyList::removeHead()
  102. {
  103.     pyListNode * pnTemp = nHead.pnNext;
  104.     nHead.pnNext = pnTemp->pnNext;
  105.     pnTemp->pnNext->pnPrev = &nHead;
  106.     if (pnTemp->pyData->bIsTemporary && (pnTemp != &nTail))
  107.         delete pnTemp;
  108.     iNumberInList--;
  109. }
  110.  
  111. //returns 1 if nHead.pnNext == &nTail (ie if the object after the head
  112. //is the tail, the list is empty) otherwise returns 0)
  113. int pyList::iIsEmpty(void)
  114. {
  115.     return nHead.pnNext == &nTail;
  116. };
  117.  
  118. //constructor for pyListIterator, initializes the list and sets iLooped
  119. //equal to zero.
  120. pyListIterator::pyListIterator(pyList & rLFromList)
  121. {
  122.     initialize(rLFromList);
  123.     iLooped = 0;
  124. };
  125.  
  126. //sets the pointers pLMyList and pnThisNode to point to rLFromList
  127. //and pLMyList->nHead.pnNext.
  128. void pyListIterator::initialize(pyList & rLFromList)
  129. {
  130.     pLMyList = &rLFromList;
  131.     pnThisNode = pLMyList->nHead.pnNext;
  132. };
  133.  
  134. //increments the listIterator to the next object in the list; if
  135. //the internal variable iLooped is nonzero and the iterator is
  136. //at the end of the list, it loops back to the first item.
  137. yakObject * pyListIterator::operator ++()
  138. {
  139.     pnThisNode = pnThisNode->pnNext;
  140.     if (iLooped)
  141.         if (!(int)(*this)) setToHead();
  142.     return pnThisNode->pyData;
  143. };
  144.  
  145. //decrements the listIterator to the previous item in the list; if the
  146. //internal variable iLooped is nonzero and the iterator is at the
  147. //head of the list, it loops back to the last item
  148. yakObject * pyListIterator::operator --()
  149. {
  150.     pnThisNode = pnThisNode->pnPrev;
  151.     if (iLooped)
  152.         if (!(char)(*this)) setToTail();
  153.     return pnThisNode->pyData;
  154. };
  155.  
  156. //returns the current data at the iterator.
  157. yakObject * pyListIterator::pyCurrent()
  158. {
  159.     return pnThisNode->pyData;
  160. };
  161.  
  162. //returns 1 if the iterator is valid, 0 if it is at the tail of the list.
  163. pyListIterator::operator int()
  164. {
  165.     return (pnThisNode != &(pLMyList->nTail));
  166. };
  167.  
  168. //returns 1 if the iterator is valid, 0 if it is at the head of the list.
  169. pyListIterator::operator char()
  170. {
  171.     return (pnThisNode != &(pLMyList->nHead));
  172. };
  173.  
  174. //sets the iterator to the head (first item) of the list
  175. void pyListIterator::setToHead()
  176. {
  177.     pnThisNode = pLMyList->nHead.pnNext;
  178. };
  179.  
  180. //sets the iterator the tail (last item) of the list
  181. void pyListIterator::setToTail()
  182. {
  183.     pnThisNode = pLMyList->nTail.pnPrev;
  184. };
  185.  
  186. //typed iterators (ptListIterator) return a pointer to the correct
  187. //type of object; ie to get an iterator that returns "int" you would
  188. //use
  189. //ptListIterator<int> thisIterator(intList);
  190.